const fs  = require('fs');
const axios  = require('axios');
const xlsx = require('node-xlsx');
const path = require('path');

const baseUrl = "https://www.17500.cn";       //Domain
const baseUrl1 = "https://yclapi.cn";       //Domain
// 读取目录
const readFile = async(file)=> {
    return new Promise((resolve, reject) => {
        fs.readFile(file, 'utf8', (err, data) => {
            if (err) {
                reject(err);
            } else {
                resolve(data);
            }
        });
    });
}
// 写入目录
const writeFile = async(file, data)=> {
    return new Promise((resolve, reject) => {
        fs.writeFile(file, data, (err) => {
            if (err) {
                reject(err);
            } else {
                resolve();
            }
        });
    });
}
const request = async (params)=>{
    const baseHead = {
        "accept-language": "zh-CN,zh;q=0.9",
        "cache-control": "no-cache",
        "pragma": "no-cache",
        "priority": "u=1, i",
        "sec-ch-ua": "\"Not)A;Brand\";v=\"99\", \"Microsoft Edge\";v=\"127\", \"Chromium\";v=\"127\"",
        "sec-ch-ua-mobile": "?0",
        "sec-ch-ua-platform": "\"Windows\"",
        "sec-fetch-dest": "empty",
        "sec-fetch-mode": "cors",
        "sec-fetch-site": "same-origin",
        "Referrer-Policy": "strict-origin-when-cross-origin",
        "user-agent": "Mozilla/5.0 (Linux; Android 5.0; SM-N9100 Build/LRX21V) > AppleWebKit/537.36 (KHTML, like Gecko) Version/4.0 > Chrome/37.0.0.0 Mobile Safari/537.36 > MicroMessenger/6.0.2.56_r958800.520 NetType/WIFI Edg/126.0.0.0"
    }
    try {
        let response = await axios(params.url, {
            // maxRedirects: 1200,
            headers: params.headers ? Object.assign(params.headers,baseHead) : {},
            body: params.body ? params.body : null,
            data: params.data ? params.data : null,
            method: params.method || 'GET'
        })
        return response;
    } catch (error) {
        return error;
      }
}
/**
 * 生成导出excel
 * @options 传入数据:
 *  options = [
        {
            "name": "表名",
            "data": [
                [标题],
                ["表头1","表头2","表头3"...]
                ["数据1","数据2","数据3"...]
                ...
            ]
        }
    ]
 */
const buildExcel = async (options)=>{
    return new Promise((resolve, reject) => {
        const buffer = xlsx.build(options);
        const fileName = `${options[0]['title']}.xlsx`;
        const filePath = path.join(__dirname,'files', fileName);
        console.log(filePath)
        fs.writeFile(filePath, buffer, function (err) {
            if (err) {
                console.log(err, "导出excel失败");
                reject(err);
            } else {
                console.log("导出excel成功!");
                resolve(filePath);
            }
        });
    })
}
const excelKayReg = [
    {key: 'qihao', name: "期号"},
    {key: 'riqi', name: "日期"},
    {key: 'red', name: "奖号"},
    {key: 'moni', name: "试机号"},
    {key: 'guanzhu', name: "关注码"},
    {key: 'total', name: "销售额"},
]

module.exports = {
    readFile,
    writeFile,
    request,
    baseUrl,
    baseUrl1,
    buildExcel,
    excelKayReg
}